package com.test.study.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Test;

public class JBDCDemo1 {

	@Test
	public void demo1() throws SQLException {
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			// 加载驱动
			Class.forName("com.mysql.cj.jdbc.Driver");
			// 获取连接
			connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mp?characterEncoding=utf8&useUnicode=true", "root", "root");
			// 操作数据库
			statement = connection.createStatement();
			statement.execute("select * from user_test");
			resultSet = statement.getResultSet();
			while (resultSet.next()) {
				String username = resultSet.getString("username");
				System.out.println(username);

			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 释放资源
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
				resultSet = null;
			}

			if (statement != null) {
				try {
					statement.close();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
				statement = null;
			}

			if (connection != null) {
				try {
					connection.close();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
				connection = null;
			}

		}

	}

}
